/**
 *====================================================
 * 文件名称: BulkstockGatherServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.bulkstock.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.bulkstock.entity.BulkstockGatherEntity;
import com.yuexunit.finance.bulkstock.entity.lexicon.BulkstockGatherAuditStateEnum;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: BulkstockGatherServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:53:27
 */
@Implements(BulkstockGatherService.class)
public class BulkstockGatherServiceImpl implements BulkstockGatherService {

	@Override
	public Long createBulkstockGather(BulkstockGatherEntity entity) {
		entity.setIssueDateTime(DateUtil.now());
		entity.setEnabled(Boolean.TRUE);
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		entity.setAuditState(BulkstockGatherAuditStateEnum.UNAUDIT);
		PersistUtil.insert(entity);
		return entity.getGatherId();
	}

	@Override
	@Transactional
	public void updateBulkstockGather(BulkstockGatherEntity entity) {
		BulkstockGatherEntity persist = inquireBulkstockGatherDetail(entity.getGatherId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	@Transactional
	public void disableBulkstockGather(Long gatherId) {
		PersistUtil.delete(BulkstockGatherEntity.class, gatherId);
	}

	@Override
	public BulkstockGatherEntity inquireBulkstockGatherDetail(Long gatherId) {
		BulkstockGatherEntity entity = PersistUtil.get(BulkstockGatherEntity.class, gatherId);
		if (entity == null || !entity.getEnabled()) {
			throw new BusinessException("该汇总价格信息不存在");
		}
		return entity;
	}

	@Override
	public PageList<BulkstockGatherEntity> inquireBulkstockGatherPageList(Long categoryId, Long bulkstockInfoId, String bulkstockName, String specification, Integer startTime, Integer endTime, String startPriceDate, String endPriceDate,
			Integer auditState, PageCriteria page) {
		DetachedCriteria dc = DetachedCriteria.forClass(BulkstockGatherEntity.class);
		dc.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (categoryId != null) {
			dc.add(Restrictions.eq("categoryId", categoryId));
		}
		if (bulkstockInfoId != null) {
			dc.add(Restrictions.eq("bulkstockInfoId", bulkstockInfoId));
		}
		if (StringUtils.isNoneBlank(bulkstockName)) {
			dc.add(Restrictions.like("bulkstockName", "%" + bulkstockName + "%"));
		}
		if (StringUtils.isNoneBlank(specification)) {
			dc.add(Restrictions.like("specification", "%" + specification + "%"));
		}
		if (startTime != null) {
			dc.add(Restrictions.ge("issueDateTime", DateUtils.formatBeginDay(String.valueOf(startTime))));
		}
		if (endTime != null) {
			dc.add(Restrictions.le("issueDateTime", DateUtils.formatEndDay(String.valueOf(endTime))));
		}
		if (startPriceDate != null) {
			dc.add(Restrictions.ge("priceDate", DateUtils.parseDateInt(startPriceDate)));
		}
		if (endPriceDate != null) {
			dc.add(Restrictions.le("priceDate", DateUtils.parseDateInt(endPriceDate)));
		}
		if (auditState != null) {
			dc.add(Restrictions.eq("auditState", BulkstockGatherAuditStateEnum.valueOf(auditState)));
		}
		dc.addOrder(Order.desc("priceDate"));
		return PersistUtil.get(dc, page);
	}

}
